package edu.fiu.cis.personalize.server.berkDB.serializers;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;

import edu.fiu.cis.personalize.server.graph.Node;
import edu.fiu.cis.personalize.server.graph.NodeType;
import edu.fiu.cis.personalize.server.graph.store.Edges;
import edu.fiu.cis.personalize.server.graph.store.NodeEdges;

public class NodeEdgesSerializer extends TupleBinding<NodeEdges> {

	private EdgesSerializer edgesSerializer = new EdgesSerializer();

	@Override
	public NodeEdges entryToObject(TupleInput t) {
		return NodeEdges.load(t.readInt(), edgesSerializer.entryToObject(t),
				edgesSerializer.entryToObject(t),
				edgesSerializer.entryToObject(t),
				edgesSerializer.entryToObject(t));
	}

	@SuppressWarnings("unchecked")
	@Override
	public void objectToEntry(NodeEdges ne, TupleOutput to) {
		to.writeInt(ne.getNodeId());
		edgesSerializer.objectToEntry(
				(Edges<Node>) ne.getEdgeMap(NodeType.USER), to);
		edgesSerializer.objectToEntry(
				(Edges<Node>) ne.getEdgeMap(NodeType.DOCUMENT), to);
		edgesSerializer.objectToEntry(
				(Edges<Node>) ne.getEdgeMap(NodeType.GROUP), to);
		edgesSerializer.objectToEntry(
				(Edges<Node>) ne.getEdgeMap(NodeType.QUERY), to);

	}

}
